package com.OutterSpace;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.scene.background.ParallaxBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;

public class ScrollableParallaxBackground extends ParallaxBackground {

	private float cameraPreviousX;
	public float cameraOffsetX;
	private float aniDuration;

	private Camera camera;
	AnimatedSprite player;

	public ScrollableParallaxBackground(final float pRed, final float pGreen,
			final float pBlue, Camera camera) {
		super(pRed, pGreen, pBlue);

		this.camera = camera;
		cameraPreviousX = camera.getCenterX();
	}
	
	public ScrollableParallaxBackground(final float pRed, final float pGreen,
			final float pBlue, AnimatedSprite player) {
		super(pRed, pGreen, pBlue);

		this.player = player;
		cameraPreviousX = player.getX();
	}

	public ScrollableParallaxBackground(final float pRed, final float pGreen,
			final float pBlue, AnimatedSprite player,  Camera camera) {
		super(pRed, pGreen, pBlue);

		this.player = player;
		this.camera = camera;
		cameraPreviousX = camera.getCenterX();
	}

	public void updateScrollEvents() {
/*		if (cameraPreviousX != this.camera.getCenterX()) {
			cameraOffsetX = cameraPreviousX - this.camera.getCenterX();
			cameraPreviousX = this.camera.getCenterX();
		}
*/	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		super.onUpdate(pSecondsElapsed);
		
		if (cameraOffsetX>0){
			this.mParallaxValue += (cameraOffsetX ) * pSecondsElapsed;
			camera.setCenter(camera.getCenterX()+(cameraOffsetX ) * pSecondsElapsed, camera.getCenterY());
			cameraOffsetX-=1; 
			player.setFlippedHorizontal(false);
			if (player.isAnimationRunning()==false)	player.animate(100);}
		else if (cameraOffsetX<0){
			this.mParallaxValue += (cameraOffsetX ) * pSecondsElapsed;
			camera.setCenter(camera.getCenterX()+(cameraOffsetX ) * pSecondsElapsed, camera.getCenterY());
			cameraOffsetX+=1; 
			player.setFlippedHorizontal(true);
			if (player.isAnimationRunning()==false)	player.animate(100);
		}
		else if (cameraOffsetX==0){player.stopAnimation(0); aniDuration=0;};
		//cameraOffsetX = 0;
    	player.setPosition(camera.getCenterX(), camera.getHeight()  -100);
	}

}